package com.woniu.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.sun.org.apache.bcel.internal.generic.RETURN;
import com.woniu.entity.Emp1;
import com.woniu.pojo.Emp;
import com.woniu.service.EmpService;
import org.apache.taglibs.standard.lang.jstl.EmptyOperator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.imageio.ImageIO;
import javax.print.DocFlavor;
import javax.servlet.http.HttpServletRequest;
import java.awt.image.BufferedImage;
import java.io.File;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("emp")
public class EmpController {

    @Autowired
    EmpService empService;


    /**
     * 跳转员工管理页面
     * */
    @RequestMapping("formList")
    public String formList(){
        return "WEB-INF/emp/empList";
    }

    /**
     * 跳转添加员工
     * */
    @RequestMapping("formAdd")
    public String formAdd(){
        return "WEB-INF/emp/add";
    }

    /**
     * 跳转修改员工
     * */
    @RequestMapping("formUpdate")
    public String formUpdate(Integer empId,Model model){
        model.addAttribute("empId", empId);
        return "WEB-INF/emp/update";
    }
    /**
     * 添加员工
     * */
    @RequestMapping("add")
    @ResponseBody
    public String add(Emp emp){
        System.out.println(emp);
//        try {
//            empService.add(emp);
//        } catch (Exception e) {
//            e.printStackTrace();
//            return "";
//        }
        return "";
    }

    /**
     * 根据ID删除员工
     * */
    @RequestMapping("deleteById")
    @ResponseBody
    public String deleteById(Integer[] empId){
        for (int e : empId) {
            System.out.println(e);
        }
        try {
            empService.deleteById(empId);
        } catch (Exception e) {
            e.printStackTrace();
            return "";
        }
        return "ok";
    }

    /**
     * 根据角色修改员工
     * */
    @RequestMapping("updateById")
    @ResponseBody
    public String updateById(Emp emp){
        System.out.println(emp);
        try {
            empService.updateById(emp);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
        return "ok";
    }

    /**
     * 上传图片
     * */
    @RequestMapping("updateImgById")
    @ResponseBody
    public Map updateImgById(Emp emp,MultipartFile file, HttpServletRequest request){

        //保存数据库的路径
        String sqlPath = null;
        try {
            //保存数据库的路径
            //定义文件保存的本地路径
            String path = request.getServletContext().getRealPath("/")+"userImg\\";
            File f=new File(path);
            if (!f.exists()) {
                f.mkdirs();
            }

            //定义 文件名
            String filename=null;
            if(!file.isEmpty()){
                //生成uuid作为文件名称
                String uuid = UUID.randomUUID().toString().replaceAll("-","");
                String fileName=file.getOriginalFilename();
                //获得文件后缀名
                String suffixName=fileName.substring(fileName.lastIndexOf(".")+1);
                //得到 文件名
                filename=uuid+"."+suffixName;
                System.out.println(filename);
                //文件保存路径
                file.transferTo(new File(path+filename));
//                File f1=new File(f,filename);
            }
            //把图片的相对路径保存至数据库
            sqlPath = "userImg/"+filename;
            System.out.println(sqlPath);
            emp.setEmpImg(sqlPath);

            empService.updateById(emp);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
        Map<String,Object> map=new HashMap<>();
        map.put("resp", "ok");
        return map;
    }

    /**
     * 查询一个员工
     * */
    @RequestMapping("findOne")
    @ResponseBody
    public String findOne(Integer empId, Model model){
        Emp1 emp=null;
        try {
            emp=empService.findOne(empId);

        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }

        return "emp";
    }

    /**
     * 查询所有员工
     * */
    @RequestMapping("findAll")
    @ResponseBody
    public Map findAll(Integer page,Integer limit,Emp emp){
        System.out.println(emp);
        Map<String,Object> map=new HashMap<>();
        PageHelper.startPage(page,limit);
        PageInfo<Emp1> pageInfo=null;
        try {
            List<Emp1> all = empService.findAll(emp);
            pageInfo=new PageInfo<Emp1>(all);
        } catch (Exception e) {
            e.printStackTrace();
        }

        map.put("data", pageInfo.getList());
        map.put("msg", "");
        map.put("code", 0);
        map.put("count", pageInfo.getTotal());
        return map;
    }


    //修改查找个人信息
    @RequestMapping("findOne1")
    @ResponseBody
    public Map<String, Object> findOne1(Integer empId){
        Map<String,Object> map=new HashMap<>();
        Emp1 emp=null;
        try {
            emp=empService.findOne(empId);
            map.put("emp",emp);
            map.put("result","ok");

        } catch (Exception e) {
            map.put("result","error");
            e.printStackTrace();

        }

        return map;
    }


    /**
     * 跳转员工修改员工自己信息
     * */
    @RequestMapping("updateJump1")

    public String updateJump1(Integer empId,Model model){
        Emp1   emp=empService.findOne(empId);
        model.addAttribute("emp", emp);
        return "WEB-INF/info/empInfoUpdate";
    }
    /**
     * 员工自己修改自己信息
     * */
    @RequestMapping("updateById1")
    @ResponseBody
    public Map<String, Object> updateById1(Emp emp){
        Map<String,Object> map=new HashMap<>();
        try {
            empService.updateById1(emp);
            map.put("result","ok");
        } catch (Exception e) {
            map.put("result","error");
            e.printStackTrace();

        }
        return map;
    }
}
